package com.portalito.android;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Camara extends SurfaceView implements SurfaceHolder.Callback {
	
	private static final String TAG = "Camara";

	private SurfaceHolder mHolder;
	private Camera camera;
	  
	public Camara(Context context) {
		super(context);
		mHolder = getHolder();
	    mHolder.addCallback(this);
	    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	public void takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback jpeg) {
		camera.takePicture(shutter, raw, jpeg);
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		camera.startPreview();
	}
	
	public void setFlashOn() {
		Parameters params = camera.getParameters();
		params.setFlashMode(Parameters.FLASH_MODE_ON);
		camera.setParameters(params);
	}
	
	public void setFlashOff() {
		Parameters params = camera.getParameters();
		params.setFlashMode(Parameters.FLASH_MODE_OFF);
		camera.setParameters(params);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		camera = Camera.open();
		Parameters params = camera.getParameters();
		params.setJpegQuality(20);
		
		List<Size> sizes = params.getSupportedPictureSizes();
		Resolucion resolucion = obtenerResolucionMinima(sizes);
		if(resolucion != null) {
			params.setPictureSize(resolucion.ancho, resolucion.alto);
		}
		
		params.setPictureFormat(PixelFormat.JPEG);
		camera.setParameters(params);
	    try {
	      camera.setPreviewDisplay(holder);

	      camera.setPreviewCallback(new PreviewCallback() {
	        public void onPreviewFrame(byte[] data, Camera camera) {
	          Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
	          Camara.this.invalidate();
	        }
	      });
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		camera.stopPreview();
		camera.setPreviewCallback(null);
		camera.release();
	    camera = null;
	}
	
	private Resolucion obtenerResolucionMinima(List<Size> list) {
		Resolucion resolucion = null;
		if(list != null) {
			if(list.size() > 0) {
				resolucion = new Resolucion();
				resolucion.ancho = list.get(0).width;
				resolucion.alto = list.get(0).height;
				for(int i = 0; i < list.size(); i++) {
					if(list.get(i).width < resolucion.ancho) {
						resolucion = new Resolucion();
						resolucion.ancho = list.get(i).width;
						resolucion.alto = list.get(i).height;
					}
				}
			}
		}
		return resolucion;
	}
	
	private class Resolucion {
		public int ancho;
		public int alto;
	}
}
